
const {
  API,
  defaultPage,
  equipStatusEnum,
  CLINIC_MANAGER_CODE,
  rootPath
} = require('../../utils/const.js');
const {
  formVaildate,
  checkCount
} = require('../../utils/formCheckUtil.js');
const {
  getRequest,
  postRequest
} = require('../../utils/request.js');
const re = require('../../utils/request.js');
const {
  EuipFormRoles
} = require('../../utils/rulse.js');
const {
  hasRole
} = require('../../utils/userUtil.js');
var app = getApp();

function uploadSignature(filePath, agreementId) {
  let token = wx.getStorageSync("token");
  let url = rootPath + API.signAgreementLetter;
  wx.uploadFile({
    url: url, // Java Spring API 接口
    filePath: filePath,
    name: 'signature',
    formData: {
      agreementId: agreementId
    },
    header: {
      'Authorization': `Bearer ${token}`  // 传递token
    },
    success: function(res) {
      console.log('Signature uploaded:', res.data);
      wx.showToast({
        title: '提交成功',
        icon: 'success',
        duration: 2000,
        complete: function() {
          // 延时跳转，确保提示显示完毕
          setTimeout(() => {
            wx.switchTab({
              url: '/pages/home/home'
            });
          }, 2000);  // 2秒钟后跳转
        }
      });
    }
  });
};

Page({
  data: {
    context: null,
    isDrawing: false,
    lastX: 0,  // 上一次绘制点的X坐标
    lastY: 0,  // 上一次绘制点的Y坐标
    agreementId: ''
  },

  onLoad: function(options) {
    const context = wx.createCanvasContext('signatureCanvas');
    context.setStrokeStyle('#000000');
    context.setLineWidth(2);
    context.setLineCap('round');  // 使线条端点圆润
    context.setLineJoin('round'); // 使线条连接处圆润

    this.setData({
      context: context
    });

    if (options.id) {
      this.setData({
        agreementId: options.id
      });
    }
  },

  touchStart: function(e) {
    const { context } = this.data;

    this.setData({
      isDrawing: true,
      lastX: e.touches[0].x,
      lastY: e.touches[0].y
    });

    context.moveTo(this.data.lastX, this.data.lastY);
  },

  touchMove: function(e) {
    if (!this.data.isDrawing) return;

    const { context, lastX, lastY } = this.data;
    const currentX = e.touches[0].x;
    const currentY = e.touches[0].y;

    context.moveTo(lastX, lastY);
    context.lineTo(currentX, currentY);
    context.stroke();
    context.draw(true); // 立即绘制

    this.setData({
      lastX: currentX,
      lastY: currentY
    });
  },

  touchEnd: function() {
    this.setData({
      isDrawing: false
    });
  },

  clearSignature: function() {
    const { context } = this.data;
    context.clearRect(0, 0, 750, 300);
    context.draw();
  },

  saveSignature: function() {
    wx.canvasToTempFilePath({
      canvasId: 'signatureCanvas',
      success: (res) => {
        console.log('Signature saved:', res.tempFilePath);
        const signatureFilePath = res.tempFilePath;
        uploadSignature(signatureFilePath, this.data.agreementId);
        /*
        var formValue = {
          'agreementId': this.data.agreementId,
          'signedName': 'signed'
        };
        postRequest(API.signAgreementLetter, formValue).then(resplus => {
          if (resplus.code === 200) {
            console.log('resplus', resplus);
            wx.showToast({
              title: '提交成功',
              icon: 'success',
              duration: 2000,
              complete: function() {
                // 延时跳转，确保提示显示完毕
                setTimeout(() => {
                  wx.switchTab({
                    url: '/pages/home/home'
                  });
                }, 2000);  // 2秒钟后跳转
              }
            });
          }
        });
        */
      },
      fail: (err) => {
        console.error('Failed to save signature:', err);
      }
    });
  },
});
